package org.example.service.impl;

import org.example.domain.Comment;
import org.example.repository.CommentRepository;
import org.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Optional;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public Comment createComment(Comment commentDTO) {
        Comment comment = new Comment();
        comment.setId(java.util.UUID.randomUUID().toString());
        comment.setUserId(commentDTO.getUserId());
        comment.setUsername(commentDTO.getUsername());
        comment.setUserAvatar(commentDTO.getUserAvatar());
        comment.setContent(commentDTO.getContent());
        comment.setPostId(commentDTO.getPostId());  // 设置帖子ID
        comment.setParentId(commentDTO.getParentId());  // 设置父评论ID
        comment.setHasLowerComment(false);
        comment.setCreateTime(new Date());
        
        // 如果有父评论ID，检查并更新父评论的状态
        if (comment.getParentId() != null) {
            Optional<Comment> parentCommentOpt = commentRepository.findById(comment.getParentId());
            if (parentCommentOpt.isPresent()) {
                Comment parentComment = parentCommentOpt.get();
                // 只有当父评论的hasLowerComment为false时才更新
                if (!parentComment.getHasLowerComment()) {
                    parentComment.setHasLowerComment(true);
                    commentRepository.save(parentComment);
                }
            }
        }
        
        return commentRepository.save(comment);
    }

    /***
     * 根据帖子ID获取其所有父级评论
     * @param postId 帖子ID
     * @return
     */
    @Override
    public List<Comment> getPostComments(String postId) {
        return commentRepository.findByPostIdAndParentIdIsNull(postId);
    }


    @Override
    public List<Comment> getCommentReplies(String commentId) {
        return commentRepository.findByParentId(commentId);
    }

    /***
     * 获取一个帖子的总评论数
     * @param postId
     * @return
     */
    @Override
    public int getTotalCountByPostId(String postId) {
        return commentRepository.countByPostId(postId);
    }

    @Override
    public List<Comment> getLowerComments(String postId, String commentId) {
        // 获取指定评论的所有子评论
        List<Comment> lowerComments = commentRepository.findByParentId(commentId);
        
        // 处理每个子评论，设置必要的属性
        for (Comment comment : lowerComments) {
            // 检查是否有下一级评论
            int replyCount = commentRepository.countByParentId(comment.getId());
            comment.setHasLowerComment(replyCount > 0);
            comment.setReplyCount(replyCount);
        }
        
        return lowerComments;
    }

    @Override
    public List<Comment> getPostCommentReplies(String postId, String commentId) {
        // 首先验证评论是否属于该帖子
        Comment parentComment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("评论不存在"));
        
        if (!parentComment.getPostId().equals(postId)) {
            throw new RuntimeException("该评论不属于指定帖子");
        }

        // 获取该评论的所有直接回复
        List<Comment> replies = commentRepository.findByParentId(commentId);
        
        // 为每个回复设置回复数
        for (Comment reply : replies) {
            long replyCount = commentRepository.countByParentId(reply.getId());
            reply.setReplyCount((int) replyCount);
        }

        return replies;
    }
}